package org.jeecg.modules.paycenter.channel.zxbank.service;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.paycenter.channel.zxbank.dto.BaseUserData;
import org.jeecg.modules.paycenter.channel.zxbank.dto.RetPacket;
import org.jeecg.modules.paycenter.channel.zxbank.dto.UserDataList;
import org.jeecg.modules.paycenter.channel.zxbank.utils.ZXUtil;
import org.jeecg.modules.paycenter.service.PayUtil;
import org.jeecg.modules.paycenter.service.WalletService;
import org.jeecg.modules.wlhy.bill.entity.HyShippingNoteBill;
import org.jeecg.modules.wlhy.bill.service.IHyDriverBillService;
import org.jeecg.modules.wlhy.bill.service.IHyShippingNoteBillService;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.hyplatform.service.IHyPlatformCostService;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
@Slf4j
public class ZXBankService{
    public static  final String CHANNEL = "2";
    @Autowired private IHyShippingNoteService hyShippingNoteService;
    @Autowired private IHyShippingNoteBillService hyShippingNoteBillService;
    @Autowired private IHyDriverBillService hyDriverBillService;
    @Autowired private IHyPlatformCostService hyPlatformCostService;
    @Autowired private PayUtil payUtil;
    @Autowired private WalletService walletService;

    @Value(value = "${jeecg.path.bank}")
    private String bankpath;

    public Result<?> downloadZxHd(List<HyShippingNoteBill> hyShippingNoteBills, Date pdate) {
        HyConfig config = HyConfigUtils.getConfig();
        if (!config.getZxIzUse()) {
            return Result.error("未开通中信银行支付服务！");
        }
        String zxAccNo = config.getZxAccNo();
        if(StringUtils.isEmpty(zxAccNo)){
            return Result.error("未开通中信银行支付服务！");
        }
        BigDecimal minAmt = null;
        BigDecimal maxAmt = null;
        for (HyShippingNoteBill hyShippingNoteBill : hyShippingNoteBills) {
            BigDecimal amt = hyShippingNoteBill.getAmt();
            if(minAmt == null){
                minAmt = amt;
                maxAmt = amt;
            }else {
                //>
                if(minAmt.compareTo(amt) == 1){
                    minAmt = amt;
                }
                //<
                if(maxAmt.compareTo(amt) == -1){
                    maxAmt = amt;
                }
            }
        }

        String pdatestr = DateUtils.formatDate(pdate, "yyyyMMdd");
        RetPacket packet = null;
        packet = ZXUtil.getHd(zxAccNo,pdatestr,minAmt,maxAmt);

        if(packet.isSuccess()){
            //解析
            UserDataList list = packet.getList();
            if(list == null ){
                return Result.error("没有可下载数据");
            }
            List<BaseUserData> rows = list.getRow();
            if(rows == null || rows.size() == 0){
                return Result.error("没有可下载数据");
            }
            Map<String,HyShippingNoteBill> billMaps = new HashMap<>();
            for (BaseUserData row : rows) {
                String brSeqNo = row.getBrseqNo2();
                String otherRaccNo1 = row.getOtherRaccNo1();
                String otherRaccNm1 = row.getOtherRaccNm1();
                BigDecimal tranAmt = row.getTranAmt();
                //查找对应的HyShippingNoteBill
                HyShippingNoteBill bill = this.getBill(hyShippingNoteBills,otherRaccNo1,otherRaccNm1,tranAmt);
                if(bill!=null){
                    billMaps.put(brSeqNo,bill);
                }
            }
            if(billMaps.size() == 0){
                return Result.error("没有可下载数据");
            }
            Set<String> brSeqNos = billMaps.keySet();
            //下载
            packet = ZXUtil.downloadHd(zxAccNo,brSeqNos);
            if(packet.isSuccess()){
                Integer size = packet.getSize();
                if(size != brSeqNos.size()){
                    return Result.error("没有可下载数据");
                }
                String dateString = DateUtils.getDate("yyyyMMdd");
                String basePath = "/zx/dh/" + dateString + "/";
                String tfilePath = this.bankpath + basePath;
                //处理pdf
                String fileName = packet.getFileName();
                String fileConTent = packet.getFileConTent();

                byte[] decode = Base64.getDecoder().decode(fileConTent);
                String zipPath =tfilePath +  fileName;
                FileUtil.writeBytes(decode,zipPath);
                ZipUtil.unzip(zipPath,tfilePath);
                FileUtil.del(zipPath);
                //更新数据
                for (String brSeqNo : brSeqNos) {
                    HyShippingNoteBill shippingNoteBill = billMaps.get(brSeqNo);
                    String bankHd = basePath + brSeqNo + ".pdf";
                    shippingNoteBill.setBankHd(bankHd);
                    hyShippingNoteBillService.updateById(shippingNoteBill);
                }
                return Result.OK("下载成功");
            }else {
                return Result.error(packet.getStatusText());
            }
        }else {
            return Result.error(packet.getStatusText());
        }
    }

    private HyShippingNoteBill getBill(List<HyShippingNoteBill> hyShippingNoteBills ,String otherRaccNo1, String otherRaccNm1,BigDecimal tranAmt){
        for (HyShippingNoteBill hyShippingNoteBill : hyShippingNoteBills) {
            String craccname = hyShippingNoteBill.getCraccname();
            String craccno = hyShippingNoteBill.getCraccno();
            BigDecimal amt = hyShippingNoteBill.getAmt();
            if(StringUtils.equals(craccname,otherRaccNm1) &&
               StringUtils.equals(craccno,otherRaccNo1) &&
                    (amt.compareTo(tranAmt) ==0)
            ){
                return hyShippingNoteBill;
            }
        }
        return null;
    }
}
